//
//  IWTabBarButton.m
//  ItcastWeibo
//
//  Created by apple on 14-5-5.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

// 图标的比例
#define IWTabBarButtonImageRatio 0.58


#import "XBTabBarButton.h" 
//#import "UIImage+MJ.h"
//#import "PJXBadge.h"
@interface XBTabBarButton()
// @property (nonatomic, strong) PJXBadge *badge;
@end

@implementation XBTabBarButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
     
    if (self) {
     
        self.imageView.contentMode = UIViewContentModeBottom;
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        self.titleLabel.font = [UIFont systemFontOfSize:12.0];
        
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, -3, 0);
        
        self.imageEdgeInsets = UIEdgeInsetsMake(2, 0, 0, 0);
        
        [self setTitleColor:[UIColor colorWithRed:193 / 255.0 green:193 / 255.0 blue:193 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        
        [self setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        
        self.contentEdgeInsets = UIEdgeInsetsMake(-10, 5, 5, 5);

        
   
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted {}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height * IWTabBarButtonImageRatio;
    return CGRectMake(0, 0, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = contentRect.size.height * (1 - IWTabBarButtonImageRatio);
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height - titleY;
    return CGRectMake(0, titleY, titleW, titleH);
}

- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    // KVO 监听属性改变
    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
    [item addObserver:self forKeyPath:@"title" options:0 context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
}

- (void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"badgeValue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}

/**
 *  监听到某个对象的属性改变了,就会调用
 *
 *  @param keyPath 属性名
 *  @param object  哪个对象的属性被改变
 *  @param change  属性发生的改变
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // 设置文字
    [self setTitle:self.item.title forState:UIControlStateSelected];
    [self setTitle:self.item.title forState:UIControlStateNormal];
    
    // 设置图片
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    
}

/*
- (NSString *)badgeValue
{
     if (self.badge) {
          return self.badge.text;
     }
     return nil;
}

- (void)setBadgeValue:(NSString *)badgeValue
{
     if (badgeValue == nil) {
          if (self.badge) {
               [self.badge removeFromSuperview];
               self.badge = nil;
          }
          return ;
     }

     if (self.badge == nil) {
          self.badge = [PJXBadge bage];

          UIView *containerView = self.iconView.icon.superview;
          if (containerView) {
               [self.badge addBadgeOnView:containerView];
          }
     }

     if (self.badge) {
          self.badge.text = badgeValue;
     }
}

- (void)playAnimation
{
     NSAssert(self.animation != nil, @"add animation in UITabBarItem");
     if (self.animation != nil && self.iconView != nil) {
          [self.animation playAnimation:self.iconView.icon textLabel:self.iconView.textLabel];
     }
}

- (void)deselectAnimation
{
     if (self.animation != nil && self.iconView != nil) {
          [self.animation deselectAnimation:self.iconView.icon textLabel:self.iconView.textLabel defaultTextColor:self.textColor];
     }
}

- (void)selectedState
{
     if (self.animation != nil && self.iconView != nil) {
          [self.animation selectedState:self.iconView.icon textLabel:self.iconView.textLabel];
     }
}*/
@end
